<?php

/**
 * Description of ReferenceAdminPresenter
 *
 * @author David Dusek
 */
class ReferenceAdminPresenter extends AdminPresenter {
    public function renderDefault() {
        $reference = new ReferenceModel($this->db);
        $this->template->references = $reference->where("agent = ?", $this->user->getId())->order("ref_order");
        $this->template->agentModel = new AgentModel($this->db);
    }
    
    public function renderVse() {
        $reference = new ReferenceModel($this->db);
        $this->template->references = $reference->order("agent");
        $this->template->agentModel = new AgentModel($this->db);
    }
    
    public function renderPridat() {
        
    }
    
    public function renderUpravit($id) {
        
    }
    
    public function handleSmazat($id) {
        $reference = new ReferenceModel($this->db);
        $res = $reference->where("id", $id)->delete();
        
        if ($res) {
            $this->flashMessage("Reference smazána");
        } else {
            $this->flashMessage("Referenci se nepodařilo odsranit. Zkuste to prosím znovu.", "error");
        }                
        $this->invalidateControl();
    }
    
    public function handlePoradi() {
        $order = $this->getParam("order");
        $orderArr = array();
        parse_str($order, $orderArr);
        
        
        foreach ($orderArr["item"] as $key => $refId) {
            $referenceModel = new ReferenceModel($this->db);            
            $referenceModel->where("id = ?", intval($refId))->update(array("ref_order" => $key + 1));
        }
        
        $this->invalidateControl("dataTable");        
    }
    
    protected function createComponentReferenceForm() {
        $form = new NAppForm();
        $form->addText("client" , "Klient:")
                ->setRequired("Jméno klienta je povinné")
                ->getControlPrototype()->class("form-wide");
        $form->addTextArea("content", "Text reference:")
                ->getControlPrototype()->class("nic-edit");        
        $form->addSubmit('send', 'Uložit');
        
        if ($this->action == "upravit") {
            $id = $this->getParam("id");
            $form->addHidden("id")->setDefaultValue($this->getParam("id"));
            
            $refModel = new ReferenceModel($this->db);
            $reference = $refModel->get($id);            
            $form->setDefaults(array(
                "client" => $reference->client,
                "content" => $reference->content,
            ));
        }

        $form->onSuccess[] = $this->referenceFormSubmitted;
        return $form;
    }
    
    /**
     *
     * @param NAppForm $form 
     */
    public function referenceFormSubmitted($form) {
        $values = $form->getValues();
        
        $refModel = new ReferenceModel($this->db);
        $maxOrder = $refModel->max("ref_order") + 1;
        
        if ($this->action == "pridat") {
            $refModel->insert(array(
                "client" => $values["client"],
                "agent" => $this->user->getId(),
                "content" => $values["content"],
                "ref_order" => $maxOrder,
            ));
            $this->flashMessage("Reference byla přidána.");
            $this->redirect("default");
        } else if ($this->action == "upravit") {
            $refModel->where("id", $values["id"])->update(array(
                "client" => $values["client"],
                "content" => $values["content"],
            ));
            $this->flashMessage("Reference byla upravena.");
            $this->redirect("this");
        }
    }
}
